//
//  ByHomeVC.m
//  jmsfhz_ios
//
//  Created by pro on 16/7/19.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByCooperationVC.h"

/** 视图 */
#import "ByCoreViewCarousel.h"          // 轮播视图

/** 单元格 */
#import "ByNotificationCell.h"          // 通知cell
#import "ByContentCell.h"               // 内容cell

@interface ByCooperationVC () <UITableViewDataSource, UITableViewDelegate, ByInformationPostQueryDelegate>
{
    NSInteger _page;        // 数据页码
}
@property (nonatomic, strong) UITableView *table;
//@property (nonatomic, strong) ByCoreViewCarousel *carouselView;

@property (nonatomic, strong) ByInformationViewModel *vm;
@property (nonatomic, strong) NSMutableArray *items;

@end

@implementation ByCooperationVC

#pragma mark - getter
- (NSMutableArray *)items {
    if (!_items) {
        _items = [[NSMutableArray alloc] init];
    }
    return _items;
}
- (ByInformationViewModel *)vm {
    if (!_vm) {
        _vm = [[ByInformationViewModel alloc] init];
        _vm.postDelegate = self;
    }
    return _vm;
}
//- (ByCoreViewCarousel *)carouselView {
//    if (!_carouselView) {
//        _carouselView = [[ByCoreViewCarousel alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_WIDTH / 2) timerDuration:2];
//    }
//    return _carouselView;
//}


#pragma mark - viewcontroller method
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createTableView];
    [self getCarouselImages];

    [self.table.mj_header beginRefreshing];

}

#pragma mark - Net request
- (void)postQuery {
    
    [self getCarouselImages];

    ByNetParamsModel *params = [[ByNetParamsModel alloc] init];
    params.cate = [NSString stringWithFormat:@"%li", NEWS_TYPE_FOR_COOPERATION];
    params.page_num = [NSString stringWithFormat:@"%li", _page];
    params.page_size = @"5";
    [self.vm postQuery:params];
}
- (void)postQuerySuccess:(ByNetBaseResult *)result {
    
    ByNetNewsModel *model = result.data;
//    ARRAR_LOG(model.results);
    
    if (_page == 1) {
        [self.items removeAllObjects];
        [self.items addObjectsFromArray:model.results];
        [self.table.mj_footer resetNoMoreData];
    } else {
        [self.items addObjectsFromArray:model.results];
    }
    
    [self.table reloadData];
    if (self.items.count == model.total.integerValue) {
        [self.table.mj_footer endRefreshingWithNoMoreData];
        [self.table.mj_header endRefreshing];
        return;
    }
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
}
- (void)postQueryFailure:(ByNetBaseResult *)result {
    [self.table.mj_footer endRefreshing];
    [self.table.mj_header endRefreshing];
    [ByCoreToolAlert alertError:result.msg];
}


#pragma mark - create view
- (void)createTableView {
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49 - 64 - 40) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
    /** 头视图 */
    UIView *view = [[UIView alloc] initWithFrame:self.carouselView.bounds];
    [view addSubview:self.carouselView];
    self.table.tableHeaderView = view;
    
    ByCoreRefreshGifFooter *footer = [ByCoreRefreshGifFooter footerWithRefreshingBlock:^{
        _page ++;
        [self postQuery];
    }];
    ByCoreRefreshGifHeader *header = [ByCoreRefreshGifHeader headerWithRefreshingBlock:^{
        _page = 1;
        [self postQuery];
    }];
    self.table.mj_footer = footer;
    self.table.mj_header = header;

}


#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.items.count;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
//    /** 通知 */
//    if (indexPath.section == 0) {
//        ByNotificationCell *cell = [ByNotificationCell createCellInTableview:tableView];
//        return cell;
//    } else {
//        ByContentCell *cell = [ByContentCell createCellInTableview:tableView cellType:ByContentCellTypeDefautl];
//        cell.model = self.items[indexPath.row];
//        return cell;
//    }
    
    ByContentCell *cell = [ByContentCell createCellInTableview:tableView cellType:ByContentCellTypeDefautl];
    cell.model = self.items[indexPath.row];
    return cell;
    
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 55;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 5;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 1) {
        return 0.5;
    } else {
        return 5;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    ByNetNewsItemModel *model = self.items[indexPath.row];
    NSString *postID = model.id;
    NSString *urlStr = [NSString stringWithFormat:@"Webview/Post/viewPost?id=%@", postID];
    ByWebViewController *vc = [[ByWebViewController alloc] initWithUrlString:urlStr];
    vc.navigationItem.title = model.post_cate_name;
    [self.navigationController pushViewController:vc animated:YES];

}




#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}



@end
